import Vue from "vue";
import VueRouter from "vue-router";
import Activity_Detail from "@/components/Activity_Detail.vue";
import login from "@/components/Login.vue";
import PostDetail from "@/components/PostDetail.vue";
import { getToken, removeToken } from "@/api/auth";
import { verifyToken } from "@/api/api";
import ossTest from "@/views/OssTest.vue";
import Home from "@/views/Home.vue";
import Dashboard from "@/components/Dashboard.vue";
import Idea from "@/components/IdeaList.vue";
import Challenge_Detail from "@/components/Challenge_Detail.vue";
import UploadChallenge from "@/components/UploadChallenge.vue";
import UploadActivity from "@/components/UploadActivity.vue";
import Job from "@/components/JobList.vue";
import JobDetail from "@/components/JobDetail.vue";
import PublishTask from "@/components/PublishTask.vue";
import PublishJob from "@/components/PublishJob.vue";
import AdminJobList from "@/components/AdminJobList.vue";
import UserSet from "@/components/UserSet.vue";
import Notifications from "@/views/Notifications.vue";
import EditJob from "@/components/EditJob.vue";
import TestScoringAPI from '@/components/TestScoringAPI.vue'

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    meta: { requiresAuth: true },
    redirect: { name: "Dashboard" },
    children: [
      {
        path: "",
        name: "Dashboard",
        component: Dashboard,
        meta: { requiresAuth: true },
      },
      {
        path: "Idea",
        name: "Idea",
        component: Idea,
        meta: { requiresAuth: true },
      },
      {
        path: "activity_detail",
        name: "activity_detail",
        component: Activity_Detail,
        meta: { requiresAuth: true },
      },
      {
        path: "post_detail",
        name: "post_detail",
        component: PostDetail,
        meta: { requiresAuth: true },
      },
      {
        path: "challenge_detail",
        name: "challenge_detail",
        component: Challenge_Detail,
        meta: { requiresAuth: true },
        props: (route) => ({ id: route.query.id }),
      },
      {
        path: "upload_challenge",
        name: "upload_challenge",
        component: UploadChallenge,
        meta: { requiresAuth: true },
      },
      {
        path: "upload_activity",
        name: "upload_activity",
        component: UploadActivity,
        meta: { requiresAuth: true },
      },
      {
        path: "job",
        name: "job",
        component: Job,
        meta: { requiresAuth: true },
      },
      {
        path: "job_detail",
        name: "job_detail",
        component: JobDetail,
        meta: { requiresAuth: true },
        props: true,
      },
      {
        path: "publish-task",
        name: "publish_task",
        component: PublishTask,
      },
      {
        path: "publish-job",
        name: "publish_job",
        component: PublishJob,
      },
      {
        path: "admin_job_list",
        name: "admin_job_list",
        component: AdminJobList,
      },
      {
        path: "user_set",
        name:"user_set",
        component: UserSet,
      },
      {
        path: "notifications",
        name: "notifications",
        component: () => import("@/views/Notifications.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "edit_job",
        name: "edit_job",
        component: EditJob,
        meta: { requiresAuth: true },
        props: (route) => ({ id: route.query.id }),
      },  
    ],
  },
  {
    path: "/ho",
    name: "home",
    component: Home,
    meta: { requiresAuth: true },
  },
  {
    path: "/login",
    name: "login",
    component: login,
  },
  {
    path: "/oss",
    name: "oss",
    component: ossTest,
  },
  {
    path: '/test-scoring-api',
    name: 'TestScoringAPI',
    component: TestScoringAPI
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach(async (to, from, next) => {
  // 获取令牌并验证身份
  const token = getToken();
  const isAuthenticated = token ? await verifyToken(token) : false;

  // 如果需要身份验证但用户未认证，则重定向到登录页
  if (
    to.matched.some((record) => record.meta.requiresAuth) &&
    !isAuthenticated
  ) {
    removeToken();
    next({
      path: "/login",
      query: { redirect: to.fullPath },
    });
  } else {
    next(); // 否则正常进行路由导航
  }
});

export default router;
